<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="超市名称" prop="supermarketId">
        <el-select v-model="queryParams.supermarketId" @keyup.enter.native="handleQuery" placeholder="请选择超市">
                <el-option
                  v-for="item in supermarketOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
      </el-form-item>
      <el-form-item label="商品数量" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入商品数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入交易金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange"
    :default-sort = "{prop: 'createTime', order: 'descending'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" v-if="false"/>
      <el-table-column label="支付宝交易号" width="190px" align="center" prop="outTradeNo" />
      <el-table-column label="商户订单号" align="center" prop="tradeNo" />
      <el-table-column label="超市名称" align="center" prop="supermarketName" />
      <el-table-column label="会员名称" align="center" prop="account"/>
      <el-table-column label="交易状态" align="center" prop="status" :sortable="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
        </el-table-column>
      <el-table-column label="订单商品数量" align="center" prop="orderNum" :sortable="true"/>
      <el-table-column label="交易金额" align="center" prop="amount"  :sortable="true"/>
      <el-table-column label="订单创建时间" align="center" prop="createTime" sortable width="180px" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="queryOrderRecord(scope.row.orderId)"
            v-hasPermi="['shop:order:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open">
  <el-table :data="orderCommodityList">
    <el-table-column property="name" label="商品名称" width="150"></el-table-column>
    <el-table-column property="picture" label="照片" width="200">
      <template slot-scope="scopephoto">
          <image-preview style="width:80px;height:80px;" :src="scopephoto.row.picture"/>
        </template>
    </el-table-column>
    <el-table-column property="price" label="单价"></el-table-column>
    <el-table-column property="num" label="商品数量"></el-table-column>
    <el-table-column 
        label="质量单位"
        align="left"
        property="qualityUnit"
        >
        <template slot-scope="scope">
    <dict-tag :options="dict.type.commodity_quality_unit" :value="scope.row.qualityUnit"/>
  </template>
      </el-table-column>
    <el-table-column property="total" label="小计">
    </el-table-column>
  </el-table>
</el-dialog>
  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  getOrderRecord} from "@/api/supermarket/order";
import { getSupermarketName } from "@/api/supermarket/supermarket";
export default {
 
  name: "Order",
  dicts: ['commodity_quality_unit','order_status'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      //超市选项
      supermarketOptions: [],
      // 订单商品列表
      orderCommodityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supermarketId: null,
        commodityName: null,
        status: null,
        orderNum: null,
        amount: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getSupermarketNameList();
    this.getList();
  },

  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        supermarketId: null,
        commodityId: null,
        status: null,
        orderNum: null,
        amount: null,
        createTime: null,
        createBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },

  
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 初始化超市名称
   getSupermarketNameList(){
    if(this.$store.getters.userId == 0){
        return;
      }
      const userId =this.$store.getters.userId;
      getSupermarketName(userId).then(response => {
        this.supermarketOptions =response.data;
       
    });
  },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.orderId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 查询订单商品记录
      queryOrderRecord(orderId){
        this.title = "订单详情";
        this.open= true;
        if (orderId != null) {
          getOrderRecord(orderId).then((response)=>{
            console.log(response.data);
            this.orderCommodityList = response.data;
          })
        }
        
      },
      

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids;
      getOrder(orderId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal
        .confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？')
        .then(function () {
          return delOrder(orderIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "shop/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
